package com.lzgcoding.onlineshopapp.feature.main

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.lzgcoding.onlineshopapp.R
import com.lzgcoding.onlineshopapp.feature.main.cart.CartScreen
import com.lzgcoding.onlineshopapp.feature.main.component.MainBottomBar
import com.lzgcoding.onlineshopapp.feature.main.dashboard.DashboardScreen
import com.lzgcoding.onlineshopapp.feature.main.nav.MainRoute
import com.lzgcoding.onlineshopapp.ui.theme.OnlineShopAppTheme

@Composable
fun MainNav(
    appNavController: NavController
) {
    val mainNavController = rememberNavController()
    val bottomMenus = remember {
        listOf(
            Pair(R.drawable.btn_1, R.string.str_home),
            Pair(R.drawable.btn_2, R.string.str_favorite),
            Pair(R.drawable.btn_3, R.string.str_Cart),
            Pair(R.drawable.btn_4, R.string.str_profile),
        )
    }
    var selectedRes by rememberSaveable {
        mutableIntStateOf(R.drawable.btn_1)
    }
    Scaffold(
        bottomBar = {
            MainBottomBar(
                selected = selectedRes,
                menus = bottomMenus
            ) { res ->
                selectedRes = res
                val route = when (res) {
                    R.drawable.btn_1 -> MainRoute.Home
                    R.drawable.btn_2 -> MainRoute.Favorite
                    R.drawable.btn_3 -> MainRoute.Cart
                    R.drawable.btn_4 -> MainRoute.Profile
                    else -> null
                }
                route?.let {
                    mainNavController.navigate(it) {
                        launchSingleTop = true
                        restoreState = true
                        popUpTo(MainRoute.Home) {
                            inclusive = false
                            saveState = true
                        }
                    }
                }
            }
        }
    ) { _ ->
        NavHost(
            navController = mainNavController,
            startDestination = MainRoute.Home
        ) {
            composable<MainRoute.Home> {
                DashboardScreen{
                    appNavController.navigate(it)
                }
            }
            composable<MainRoute.Favorite> {
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "favorite")
                }
            }
            composable<MainRoute.Cart> { CartScreen() }
            composable<MainRoute.Profile> {
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "profile")
                }
            }
        }
    }
}

@Preview(showBackground = true, showSystemUi = true)
@Composable
private fun MainNavPreview() {
    OnlineShopAppTheme {
        MainNav(appNavController = rememberNavController())
    }
}